<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require dirname(__FILE__).'/pc_controller.php';

class Customer extends PC_controller {

    public function __construct(){
        parent::__construct();
        $this->load->model('Customermodel','m_customer',true);
        $this->load->model('Templatemodel','m_template',true);
    }

    public function index(){
        $data = parent::index();

        $data['attr'] = array();
        $data['attr']['template_list'] = $this->m_template->getActiveTemplateList();

        $this->load->view('admin/customer',$data);
    }

    public function getCustomerList(){
        $assist['where'] = getFilter($this->input->post(),Customermodel::$searchField);
        $assist['like'] = getFilter($this->input->post(),Customermodel::$likeField);
        $assist['sort'] = getSort($this->input->post(),Customermodel::$sortField);
        $assist['pagination'] = getPagination($this->input->post());

        list($list,$count) = $this->m_customer->getCustomerListSection($assist);

        $this->response->setSuccess();
        $this->response->setItems($list);
        $this->response->setTotalCount($count);
        $this->response->responseExit();
    }


    public function getHistoryList(){
      $this->validate->setRule('customer_id','integer');
  		if(!$this->validate->run($this->input->post())){
  			$this->response->setDetail($this->validate->getMessage());
  			$this->response->responseExit();
  		}

      $assist['where'] = getFilter($this->input->post(),Customermodel::$searchField);
      $assist['like'] = getFilter($this->input->post(),Customermodel::$likeField);
      $assist['sort'] = getSort($this->input->post(),Customermodel::$sortField);
      $assist['pagination'] = getPagination($this->input->post());

      $assist['where']['customer_id'] = $info['customer_id'];

      list($list,$count) = $this->m_customer->getHistoryListSection($assist);

      $this->response->setSuccess();
      $this->response->setItems($list);
      $this->response->setTotalCount($count);
      $this->response->responseExit();
    }

	public function disableCustomer(){
		$this->validate->setRule('customer_id','integer');
		if(!$this->validate->run($this->input->post())){
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}
		$info = $this->validate->getResult();
		$info['user_id'] = $this->m_app->getCurrentUserId();
		$info['customer_status'] = STATUS_DISABLE;

		$this->m_customer->editCustomer($info);


		$this->response->setSuccess();
		$this->response->responseExit();
	}

	public function activeCustomer(){
		$this->validate->setRule('customer_id','integer');
		if(!$this->validate->run($this->input->post())){
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}
		$info = $this->validate->getResult();
		$info['user_id'] = $this->m_app->getCurrentUserId();
		$info['customer_status'] = STATUS_ACTIVE;

		$this->m_customer->editCustomer($info);


		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}

  public function sysnCustomerFromWx() {
    $this->m_customer->sysnCustomerFromWx();
  }

  public function sendHongbao() {
    $this->validate->setRule('customer_id','integer');
    $this->validate->setRule('openid','string');
    $this->validate->setRule('template_id','integer');
    $this->validate->setRule('history_money','float');
    $this->validate->setRule('history_name','string');
    $this->validate->setRule('history_support','string');
		$this->validate->setRule('history_wish','string');
		if(!$this->validate->run($this->input->post())) {
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}
    $info = $this->validate->getResult();

    if($info['history_money'] < 1) {
      $this->response->setDetail('金额不能小于1块');
      $this->response->responseExit();
    }

    if($this->m_customer->checkTemplateSend($info['customer_id'],$info['template_id'])) {
      $this->response->setDetail('该用户今天已发此模块的红包!');
      $this->response->responseExit();
    }


    include dirname(BASEPATH)."/symj/wxHongBao/oauth2.php";

    $wxapi=new Wxapi();

    $key=SALF_CODE;
    $openid = $info['openid'];//接收红包用户的openid    必须参数
    $openid_md5 = md5($info['openid'].$key); //接收红包用户的openid的md5校验码   必须参数
    $db = intval($info['history_money'] * 100); //红包金额,单位分  必须参数
    $db_md5 = md5($db.$key); //红包金额md5校验码   必须参数
    $send_name = $info['history_support']; //提供方名称   必须参数
    $wishing = $info['history_wish'];//红包祝福语   必须参数

    $result = ONLINE ? json_decode(json_encode($wxapi->pay($openid, $db, $send_name, $wishing)),true) : array("err_code" => 'SUCCESS',"status" => 'off');
    if($result['err_code'] != 'SUCCESS') {
      $this->response->setDetail($result['err_code_des']);
      $this->response->setAddon(array(
        'result' => $result,
      ));
  		$this->response->responseExit();
    }
    $info['history_result'] = json_encode($result);


    $info['history_time_create'] = NOW;
    $info['user_id'] = $this->m_app->getCurrentUserId();
    $this->m_customer->createHistory($info);
    $sql1 = $this->db->last_query();
    // echo $this->db->last_query();
    // die();
    $this->m_customer->updateCustomerHbData($info['customer_id']);
    // $sql2 = $this->db->last_query();

    $this->response->setSuccess();
    $this->response->setAddon(array(
      'result' => $result,
      // 'sql1' => $sql1,
      // 'sql2' => $sql2,
    ));
		$this->response->responseExit();
  }

}
/* End of file api_log.php */
/* Location: ./application/controllers/api_log.php */
